import request from '@/api/axios'
import {AxiosPromise} from 'axios'
import {DataRes, Paging, SeleteData} from '@/api/config'



//表头按钮定义
export type TableOperation='add'

// 知识库基本信息
export interface BaseKnowlegeInfor{
  name:string //规则名称
  grouping:string //组别
  isException:boolean //纳入异常统计
  isExceptionName:string
  remark:string //规则描述
}
export interface KnowlegeInfor extends BaseKnowlegeInfor{
  id:number
}
export type KnowlegeFormInfor = Partial<KnowlegeInfor>
export interface KnowlegeInforList {
  size:number
  total:number
  records:Array<KnowlegeInfor>
}

//知识库分页查询数据定义
export interface SearchFormData{ //extends Paging{
  name?:string|null,
  grouping?:string|null,
  isException?:boolean|null,
}
//获取知识库列表
export const getKnowlegeList = (params:SearchFormData & Paging):AxiosPromise<DataRes<KnowlegeInforList>> => {
  return request({
    url:'/api/knowledgeRule/page',
    method:'get',
    params
  })
}

//保存修改知识库
export const saveKnowlegeInfor = (data:KnowlegeFormInfor):AxiosPromise<DataRes<undefined>> => {
  return request({
    url:'/api/knowledgeRule/saveOrUpdate',
    method:'post',
    data
  })
}

// 删除知识库
export const deleteKnowlege = (id:number):AxiosPromise<DataRes<undefined>> => {
  return request({
    url:'/api/knowledgeRule/delete/' + id,
    method:'post'
  })
}


interface ItemListItem {
  id:string
  itemName:string
  resultType:number
  checkType:number
  unit:number
}
interface TargetItem{
  rangeMinInput?:string
  rangeMaxInput?:string
  rangeMinSelect?:string
  rangeMaxSelect?:string
  selected?:string
}
//判断规则-最小规则
export interface KnowlegeRuleItem{
  id:number|undefined
  ruleJudgeId:	number|undefined	//判断规则id
  inspectCategoryId:number|undefined //体检大类id
  itemGroupId:number|undefined //字典分类编码-	01:实验室相关类	02:判断类	03检验项目相关类	04:机构相关类	05:用户相关类
  itemGroupList?:Array<SeleteData>
  itemId:number|undefined //项目id
  itemList?:Array<ItemListItem>
  itemObj:ItemListItem|null
  target:string	//指标（存json字符串）
  targetObj:TargetItem //指标json
  targetType?:number
  gender?:number //性别(字典)
  ageRangeLow?:string //年龄下限值
  ageRangeHigh?:string //年龄上限值
}
//判断规则-最小规则组
export interface KnowlegeRuleItemGroup{
  id:number|null
  ruleId:number|null //知识库规则id
  type:0|1|null // 判断规则且0或1（忽略第一条）
  sort:number|null //排序
  details:Array<KnowlegeRuleItem>
}

//知识库详细信息
export interface KnowlegeData {
  id?:string,
  name:string,
  groupingName:string,
  isException:boolean,
  modules:Array<KnowlegeRuleItemGroup>, //判断规则
  healthJudgment:string //健康评价
  advise:string //建议
  healthGuide:string //健康指导
  healthGuideArr:Array<string> //健康指导选中的值
  guideDisplayValue:string //健教指导内容
  guideCheckboxValue:string //健教指导内容编辑json字符串
  dangerousControl:string //危险因素控制
  dangerousControlObj:{ //危险因素控制选中的值及填写的内容
    selected:Array<string>,
    vaccines_info:string,
    other_info:string
  },
}
export type KnowlegeDataForm = Partial<KnowlegeData>

export const getKnowlegeData = (id:string):AxiosPromise<DataRes<KnowlegeData>> => {
  return request({
    url:'/api/knowledgeRule/detail/' + id,
    method:'get'
  })
}
export const saveKnowlegeData = (data:KnowlegeDataForm):AxiosPromise<DataRes<undefined>> => {
  return request({
    url:'/api/knowledgeRule/config',
    method:'post',
    data
  })
}

//体检大类
interface CategoryItem {
  id:string
  categoryName:string
}
//体检大类列表
export const getCategoryList = ():AxiosPromise<DataRes<Array<CategoryItem>>> => {
  return request({
    url:'/api/inspectCategory/list',
    method:'get'
  })
}

interface itemGroup {
  id:string
  itemGroupName:string
}
//根据体检大类查询对应的项目组合
export const getItemGroupList = (inspectCategoryId:number):AxiosPromise<DataRes<Array<itemGroup>>> => {
  return request({
    url:'/api/itemGroup/byInspectCategory/' + inspectCategoryId,
    method:'get'
  })
}


//根据项目组合id获取体检项目
export const getItemList = (params:{itemGroupId:number}):AxiosPromise<DataRes<Array<ItemListItem>>> => {
  return request({
    url:'/api/item/getItemsByItemGroup',
    method:'get',
    params
  })
}

interface GuideItem{
  id:string
  name?:string
  healthValue?:string
  disabled?:boolean
}
export interface GuideListItem {
  id:string
  name:string
  healthContentList?:Array<GuideItem>
  disabled?:boolean
}
export const getWestDoctorKnowledgeRuleList = ():AxiosPromise<DataRes<Array<GuideListItem>>> => {
  return request({
    url:'/api/healthGuide/westDoctorKnowledgeRuleList',
    method:'get'
  })
}




